package com.ke.jun.bg.testpetpackmvvm.viewmodel.request

import androidx.lifecycle.MutableLiveData
import com.ke.jun.bg.petpackmvvm.base.viewmodel.BaseViewModel
import com.ke.jun.bg.petpackmvvm.ext.request
import com.ke.jun.bg.testpetpackmvvm.app.ext.loadListData
import com.ke.jun.bg.testpetpackmvvm.app.network.apiService
import com.ke.jun.bg.testpetpackmvvm.app.network.stateCallback.CollectUiStae
import com.ke.jun.bg.testpetpackmvvm.app.network.stateCallback.ListDataUiState
import com.ke.jun.bg.testpetpackmvvm.data.model.bean.CollectResponse
import com.ke.jun.bg.testpetpackmvvm.data.model.bean.CollectUrlResponse

/**
 * 收藏页面的viewModel
 */
class RequestCollectViewModel:BaseViewModel() {
    private var pageNo=0
    //收藏文章
    val collectUiStat:MutableLiveData<CollectUiStae> = MutableLiveData()
    //收藏网址
    var collectUrlState:MutableLiveData<CollectUiStae> = MutableLiveData()
    //收藏文章的数据
    var aritcileDataStae:MutableLiveData<ListDataUiState<CollectResponse>> = MutableLiveData()

    //收藏的的地址
    var urlDataStae:MutableLiveData<ListDataUiState<CollectUrlResponse>> = MutableLiveData()


    /**
     * 收藏 文章
     * 提醒一下，玩安卓的收藏 和取消收藏 成功后返回的data值为null，所以在CollectRepository中的返回值一定要加上 非空？
     * 不然会出错
     */
    fun collect(id:Int){
        request({ apiService.collect(id)},{
            val uiState=CollectUiStae(isSuccess = true,collect = true,id=id)
            collectUiStat.value= uiState
        },{
            val uiStae=CollectUiStae(isSuccess = false,collect = false,errorMsg = it.errorMsg,id = id)
            collectUiStat.value=uiStae
        })
    }

    /**
     * 取消收藏文章
     * 提醒一下，玩安卓的收藏 和取消收藏 成功后返回的data值为null，所以在CollectRepository中的返回值一定要加上 非空？
     * 不然会出错
     */
    fun uncollect(id: Int) {
        request({ apiService.uncollect(id) }, {
            val uiState = CollectUiStae(isSuccess = true, collect = false, id = id)
            collectUiStat.value = uiState
        }, {
            val uiState =
                CollectUiStae(isSuccess = false, collect = true, errorMsg = it.errorMsg, id = id)
            collectUiStat.value = uiState
        })
    }
    /**
     * 收藏 网址
     * 提醒一下，玩安卓的收藏 和取消收藏 成功后返回的data值为null，所以在CollectRepository中的返回值一定要加上 非空？
     * 不然会出错
     */
    fun collectUrl(name: String,link:String){
        request({ apiService.collectUrl(name,link)},{
            val uiStae=CollectUiStae(isSuccess = true,collect = true,id = it.id)
            collectUrlState.value=uiStae
        },{
            val uiStae=CollectUiStae(isSuccess = false,collect = false,errorMsg = it.errorMsg,id = 0)
            collectUrlState.value=uiStae
        })
    }


    /**
     * 收藏 网址
     * 提醒一下，玩安卓的收藏 和取消收藏 成功后返回的data值为null，所以在CollectRepository中的返回值一定要加上 非空？
     * 不然会出错
     */
    fun uncollectUrl(id: Int){
        request({ apiService.uncollect(id)},{
            val uiStae=CollectUiStae(isSuccess = true,collect = false,id = id)
            collectUrlState.value=uiStae
        },{
            val uiStae=CollectUiStae(isSuccess = false,collect = true,errorMsg = it.errorMsg,id = 0)
            collectUrlState.value=uiStae
        })
    }


    /**
     * 请求首页的数据
     */

    fun getCollectAritcleData(isRefresh:Boolean){
        if (isRefresh){
            pageNo=0
        }
        request({ apiService.getCollectData(pageNo)},{
            //请求成功
            pageNo++
            val  listDataUiState=
                ListDataUiState(
                    isSuccess = true,
                    isRefresh = isRefresh,
                    isEmpty = it.isEmpty(),
                    hasMore = isRefresh&&it.isEmpty(),
                    listData =it.datas
                )
            aritcileDataStae.value=listDataUiState
        },{
            //请求失败
            val  listDataUiState=
                ListDataUiState(
                    isSuccess = false,
                    errMessage = it.errorMsg,
                    isRefresh = isRefresh,
                    listData = arrayListOf<CollectResponse>()
                )
            aritcileDataStae.value=listDataUiState
        })
    }


    fun getCollectUrlData(){
        request({
            apiService.getCollectUrlData()
        },{
            it.map { if (it.order==0) it.order=1 }
            val listDataUiState=ListDataUiState(
                isRefresh =true,
                isSuccess = true,
                hasMore =false,
                isEmpty = it.isEmpty(),
                listData = it
            )
            urlDataStae.value=listDataUiState
        },{
            val listDataUiState=ListDataUiState(
                isSuccess = false,
                errMessage = it.errorMsg,
                listData = arrayListOf<CollectUrlResponse>()
            )

            urlDataStae.value=listDataUiState
        })
    }
}